package com.trkj.base;

import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;

import com.shamanland.fonticon.FontIconTypefaceHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by LiZanhong on 2015/06/15.
 */
public class Constants {

    public static final String FAILURE_JSON = "{\"data\":[],\"success\":false}";

    public static class Intent{
        public static final String INTENT_ACTIVITY_API = "ApiActivity";
    }

    public static class Share{
        /*技术文章类型*/
        public static final int SHARE_TYEP_TECHNOLOGY = 2;
        /*代码分享类型*/
        public static final int SHARE_TYEP_CODE = 1;
        /*IT励志类型*/
        public static final int SHARE_TYEP_IT = 3;
        /*休闲搞笑类型*/
        public static final int SHARE_TYEP_HAVE_A_REST = 4;
    }

    public static class ApiUrl{
        public static final String HTTP_TR_API_GSON_CN = "http://tr.api.gson.cn";
        public static final String HTTP_GSON_CN = "http://gson.cn";

        //http://tr.api.gson.cn/article/list/文章类型id?page=1&rows=10
        public static final String SHARE_TECHNOLOGY_ARTICLE =
                HTTP_TR_API_GSON_CN + "/article/list/{0,number,#}?page={1}&rows={2}";

        //http://tr.api.gson.cn/article/show/文章id
        public static final String SHARE_ARTICLE_DETAIL =
                HTTP_TR_API_GSON_CN + "/article/show/{0,number,#}";

        //http://tr.api.gson.cn/system/share/link/部门id?page=1&rows=10
        public static final String DOCUMENT_LINK =
                HTTP_TR_API_GSON_CN + "/system/share/link/{0,number,#}?page={1}&rows={2}";

        //http://tr.api.gson.cn/system/login
        public static final String LOGIN = HTTP_TR_API_GSON_CN + "/system/login";

        //http://tr.api.gson.cn/student/practice/4
        public static final String NOTMAL_PRACTICE =
                HTTP_TR_API_GSON_CN + "/student/practice/{0,number,#}/{1,number,#}";

        public static final String EXPIRE_PRACTICE =
                HTTP_TR_API_GSON_CN + "/student/practice/overdue/{0,number,#}/{1,number,#}";

        public static final String NOTMAL_EXAM =
                HTTP_TR_API_GSON_CN + "/student/exam/{0,number,#}/{1,number,#}";

        public static final String EXPIRE_EXAM =
                HTTP_TR_API_GSON_CN + "/student/exam/overdue/{0,number,#}/{1,number,#}";

        public static final String GET_USER_DETAIL =
                HTTP_TR_API_GSON_CN + "/system/user/{0,number,#}";

        //http://tr.api.gson.cn/system/change/password/22
        public static final String UPDATE_PASSWORD =
                HTTP_TR_API_GSON_CN + "/system/change/password/{0,number,#}";

        //http://tr.api.gson.cn/system/notification
        public static final String NOTICE_LIST =
                HTTP_TR_API_GSON_CN + "/system/notification";

        //http://tr.api.gson.cn/student/answer/22?page=1&rows=10
        public static final String QUESTION =
                HTTP_TR_API_GSON_CN + "/student/ask/{0,number,#}?page={1}&rows={2}";

        //http://tr.api.gson.cn/student/answer/22?page=1&rows=10
        public static final String REPLY =
                HTTP_TR_API_GSON_CN + "/student/answer/{0,number,#}?page={1}&rows={2}";

        //http://tr.api.gson.cn/student/points/22
        public static final String SCORE =
                HTTP_TR_API_GSON_CN + "/student/points/{0,number,#}";

        //http://tr.api.gson.cn/system/feedback/22
        //提意见和显示意见都是一个地址，不同的method
        public static final String FEEDBACK =
                HTTP_TR_API_GSON_CN + "/system/feedback/{0,number,#}";

        //http://tr.api.gson.cn/star/list?page=1&rows=20
        public static final String ALL_STAR =
                HTTP_TR_API_GSON_CN + "/star/list?page={0}&rows={1}";

        //http://tr.api.gson.cn/star/my/22
        public static final String MY_STAR =
                HTTP_TR_API_GSON_CN + "/star/my/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/exam/year/22
        public static final String CHART_YEAR_EXAM =
                HTTP_TR_API_GSON_CN + "/student/chart/exam/year/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/score/22
        public static final String CHART_SCORE_LEVEL =
                HTTP_TR_API_GSON_CN + "/student/chart/score/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/exam/finished/22
        public static final String CHART_EXAM_RATE =
                HTTP_TR_API_GSON_CN + "/student/chart/exam/finished/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/login/week/22
        public static final String CHART_LOGIN_WEEK =
                HTTP_TR_API_GSON_CN + "/student/chart/login/week/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/login/month/22
        public static final String CHART_LOGIN_MONTH =
                HTTP_TR_API_GSON_CN + "/student/chart/login/month/{0,number,#}";

        //http://tr.api.gson.cn/student/chart/login/year/22
        public static final String CHART_LOGIN_YEAR =
                HTTP_TR_API_GSON_CN + "/student/chart/login/year/{0,number,#}";

        //http://tr.api.gson.cn/student/points/rank/1
        public static final String CHART_DEPT_RANK =
                HTTP_TR_API_GSON_CN + "/student/points/rank/{0,number,#}";

        //http://tr.api.gson.cn/student/points/total/22
        public static final String CHART_SCORE_TOTAL =
                HTTP_TR_API_GSON_CN + "/student/points/total/{0,number,#}";

        //http://tr.api.gson.cn/system/update/user/22
        public static final String UPDATE_UEER_INFO =
                HTTP_TR_API_GSON_CN + "/system/update/user/{0,number,#}";

        //http://tr.api.gson.cn/student/show/exam/22/30
        public static final String EXAM_DETAIL =
                HTTP_TR_API_GSON_CN + "/student/show/exam/{0,number,#}/{1,number,#}";

        //http://tr.api.gson.cn/article/search
        public static final String SEARCH =
                HTTP_TR_API_GSON_CN + "/article/search";

        //http://gson.cn/upload/upgrade/version.txt
        public static final String CHECK_VERSION =
                HTTP_GSON_CN + "/upload/upgrade/version.txt";

        //http://gson.cn/upload/upgrade/upgrade.html
        public static final String TAORUIYUN_RELEASE_APK =
                HTTP_GSON_CN + "/upload/upgrade/upgrade.html";

        //http://tr.api.gson.cn/message/push
        //post method
        public static final String SAVE_PUSH_MESSAGE_TO_SERVER =
                HTTP_TR_API_GSON_CN + "/message/push";

        //http://tr.api.gson.cn/message/list/22
        public static final String PUSH_MESSAGE_LISH =
                HTTP_TR_API_GSON_CN + "/message/list/{0,number,#}?rows=100";

        //http://tr.api.gson.cn/message/read/22/md5
        public static final String PUSH_MESSAGE_STATUS_CHANGED =
                HTTP_TR_API_GSON_CN + "/message/read/{0,number,#}/{1}";

        //http://tr.api.gson.cn/student/points/rank/1
        public static final String DEPT_SCORE_RANK =
                HTTP_TR_API_GSON_CN + "/student/points/rank/{0,number,#}";

        /**
         * 去掉网页中的所有图片
         * @param html
         * @return
         */
        public static String deleteAllImage(String html){
            if(html == null) return null;
            return html.replaceAll("<(?i)img.*?>", "");//(?i) 不区分大小写
        }

        /**
         * 将html中的图片地址加上域名<br>
         *     如：/upload/ueditor/jsp/upload/image/20150609/1433822878829091825.png，变成<br>
         *     http://gson.cn/upload/ueditor/jsp/upload/image/20150609/1433822878829091825.png<br>
         *     使用正则表达式："(?<=src=\").+?(?=\")"
         * @param html
         * @return
         */
        public static String addDomainForImage(String html){
            if(html == null) return html;

            Pattern p = Pattern.compile("(?<=src=\").+?(?=\")");
            Matcher m = p.matcher(html);
            List<String> src = new ArrayList<String>();
            List<String> dest = new ArrayList<String>();
            while(m.find()){
                String found = m.group();
                if(found.startsWith("/upload")) {
                    src.add(found);
                    dest.add("http://gson.cn" + found);
                }
            }

            for(int i = 0; i < src.size(); i ++){
                html = html.replaceAll(src.get(i), dest.get(i));
            }

            return html;
        }
    }

    public static class Color{
        public static int getColor(Context context, int resId){
            return context.getResources().getColor(resId);
        }
    }

    public static class ResString{
        public static String getString(Context context, int resId){
            return context.getResources().getString(resId);
        }
    }

    public static class Font{
        public static Typeface getAwesomeFont(Context context){
            return FontIconTypefaceHolder.getTypeface();
        }

        public static void setAwesomeFont(Context context, TextView view){
            view.setTypeface(getAwesomeFont(context));
        }
    }

    public static class MenuID{
        public static final int MENU_ADD_FEEDBACK = 0x100;
        public static final int MENU_EXAM_INFO = 0x101;
        public static final int MENU_EXAM_SCORE = 0x102;
    }

    public static class ActivityCode{
        public static final int FEEDBACK_ADD_REQUEST_CODE = 0x200;
        public static final int LOGON_REQUEST_CODE = 0x202;
        public static final int PHOTO_REQUEST_CODE = 0x204;
        public static final int CAMERA_REQUEST_CODE = 0x205;
        public static final int SETUP_REQUEST_CODE = 0x206;
        public static final int PHOTO_CUT_REQUEST_CODE = 0x207;
    }

    public static class QuestionType{
        public static final int SELECT = 0;
        public static final int JUDGE = 1;
        public static final int PROGRAM = 2;
    }
}
